//
//  NoteViewController.m
//  Digital Seamonsters
//
//  Created by Adam Cue on 2/3/11.
//  Copyright Adam Cue 2011.
//
//  This program is licensed and distributed under the terms of the GNU General Public License.  
//	The text for the license can be found in the license.txt file in this project.
//

#import "NoteViewController.h"

#define TEXT_VIEW_FRAME       CGRectMake(5, 5, 310, 392)

@interface NoteViewController (Private)
#pragma mark SetupMethods
- (void)setupTextView;
- (void)setupToolbar;
#pragma mark UserActions
- (void)trash;
- (void)action;
@end

@implementation NoteViewController

@synthesize initialText = _initialText;
@synthesize textView = _textView;
@synthesize trashButton = _trashButton;
@synthesize actionButton = _actionButton;
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil text:(NSString *)selectedText{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       self.initialText = selectedText;
    }
    return self;
}

- (void)viewWillDisappear:(BOOL)animated{
	if(!deleted)
		[delegate noteDismissed:self.textView.text];
}

- (void)dealloc {
   self.initialText = nil;
   self.textView = nil;
   self.trashButton = nil;
	self.actionButton = nil;
   self.delegate = nil;
   
   [super dealloc];
}

- (void)viewDidLoad {
	self.view.backgroundColor = [UIColor colorWithRed:1.0 green:0.8314 blue:0.1647 alpha:1.0];
	[self setupTextView];
   [self setupToolbar];
   [super viewDidLoad];
}

#pragma mark SetupMethods
- (void)setupTextView {
   _textView = [[UITextView alloc] initWithFrame:TEXT_VIEW_FRAME];
   self.textView.text = self.initialText;
	self.textView.font = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size:16];
	self.textView.backgroundColor = [UIColor clearColor];
	[self.view addSubview:self.textView];
}

- (void)setupToolbar {
   UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 295, 320, 44)];
	toolbar.barStyle = UIBarStyleBlackOpaque;
	_trashButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trash)];
	self.trashButton.style = UIBarButtonItemStyleBordered;
	UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
	_actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(action)];
	self.actionButton.style = UIBarButtonItemStyleBordered;
	toolbar.items = [NSArray arrayWithObjects:self.actionButton, flexibleSpace, self.trashButton, nil];
	[self.view addSubview:toolbar];
	[flexibleSpace release];
	[toolbar release];
}

#pragma mark UserActions
- (void)trash{
	UIActionSheet *deleteSheet = [[UIActionSheet alloc] initWithTitle:@"Delete Note?" 
                                                            delegate:self 
                                                   cancelButtonTitle:@"Cancel" 
                                              destructiveButtonTitle:@"Delete" 
                                                   otherButtonTitles:nil];
	deleteSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
	[deleteSheet showFromBarButtonItem:self.trashButton animated:YES];
	[deleteSheet release];
}

- (void)action{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                            delegate:self 
                                                   cancelButtonTitle:@"Cancel" 
                                              destructiveButtonTitle:nil 
                                                   otherButtonTitles:@"Email", nil];
	actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
	[actionSheet showFromBarButtonItem:self.actionButton animated:YES];
	[actionSheet release];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
	if(buttonIndex == actionSheet.destructiveButtonIndex){
		deleted = YES;
		[delegate noteTrashed];
	}
	else if(buttonIndex == 0){
		[delegate noteEmailed:self.textView.text];
	}
}


@end
